<template>
  <div class="main-content">
    <!-- 列表页 -->
    <div v-if="showFlag">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-row :gutter="20" class="slt">
          <el-form-item label="用户名">
            <el-input prefix-icon="el-icon-search" v-model="searchForm.nickname" placeholder="用户名" clearable></el-input>
          </el-form-item>
          <el-form-item label="评论内容">
            <el-input prefix-icon="el-icon-search" v-model="searchForm.content" placeholder="评论内容" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="success" @click="search()">查询</el-button>
          </el-form-item>
        </el-row>

        <el-row class="ad">
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-plus"
              @click="addOrUpdateHandler()"
            >新增</el-button>
            <el-button
              :disabled="dataListSelections.length <= 0"
              type="danger"
              icon="el-icon-delete"
              @click="deleteHandler()"
            >删除</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <div class="table-content">
        <el-table class="tables"
            :size="'medium'"
            :show-header="true"
            :border="true"
            :fit="true"
            :stripe="true"
            :style="{width: '100%',fontSize:'14px',color:'#606266'}"
            :data="dataList"
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandler">
            <el-table-column
                type="selection"
                align="center"
                width="50">
            </el-table-column>
            <el-table-column label="索引" align="center" type="index" width="50" />
            <el-table-column align="center"
                prop="nickname"
                header-align="center"
                label="用户名">
                <template slot-scope="scope">
                    {{scope.row.nickname}}
                </template>
            </el-table-column>
            <el-table-column align="center"
                prop="content"
                header-align="center"
                label="评论内容">
                <template slot-scope="scope">
                    {{scope.row.content}}
                </template>
            </el-table-column>
            <el-table-column align="center"
                prop="reply"
                header-align="center"
                label="回复内容">
                <template slot-scope="scope">
                    {{scope.row.reply}}
                </template>
            </el-table-column>
            <el-table-column width="300" align="center"
                header-align="center"
                label="操作">
                <template slot-scope="scope">
                    <div class="operation-buttons">
                        <el-button type="success" icon="el-icon-tickets" size="mini" @click="addOrUpdateHandler(scope.row.id,'info')">详情</el-button>
                        <el-button  type="primary" icon="el-icon-edit" size="mini" @click="addOrUpdateHandler(scope.row.id)">回复</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteHandler(scope.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
          clsss="pages"
          :layout="layouts"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          :total="totalPage"
          class="pagination-content"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加/修改页面  将父组件的search方法传递给子组件-->
    <add-or-update v-if="addOrUpdateFlag" :parent="this" ref="addOrUpdate"></add-or-update>
  </div>
</template>
<script>
import axios from 'axios'
import AddOrUpdate from "./add-or-update";
export default {
  data() {
    return {
      searchForm: {
        key: ''
      },
      form: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      showFlag: true,
      sfshVisiable: false,
      shForm: {},
      chartVisiable: false,
      addOrUpdateFlag:false,
      layouts: 'total, sizes, prev, pager, next, jumper',
    };
  },
  created() {
    this.init();
    this.getDataList();
  },
  mounted() {
    console.log(this.$route.query.refid)
  },
  filters: {
    htmlfilter: function (val) {
      return val.replace(/<[^>]*>/g).replace(/undefined/g,'');
    }
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    init() {
    },
    search() {
      this.pageIndex = 1;
      this.getDataList();
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandler(val) {
      this.dataListSelections = val;
    },
    // 添加/修改
    addOrUpdateHandler(id, type) {
      this.showFlag = false;
      this.addOrUpdateFlag = true;
      this.crossAddOrUpdateFlag = false;
      if (type == 'info') {
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, 'info');
        });
      } else if (type == 'cross') {
        var obj = this.$storage.getObj('crossObj');
        if (obj) {
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(id, 'cross');
          });
        }
      } else {
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      }
    },
    // 查看评论
    disscussListHandler(id) {
      this.$router.push({
        path: `/discussdiscussjijieshipu`,
        query: {refid: id}
      });
    },
    // 下载
    download(file) {
      window.open(`${file}`);
    },
    // 删除
    deleteHandler(id) {
      var ids = id
          ? [id]
          : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: 'discussjijieshipu/delete',
          method: 'post',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.search();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 获取数据列表
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let params = {
        page: this.pageIndex,
        limit: this.pageSize,
        sort: 'id',
        refid : this.$route.query.refid,
      }
      if(this.searchForm.nickname!='' && this.searchForm.nickname!=undefined){
        params['nickname'] = '%' + this.searchForm.nickname + '%'
      }
      if(this.searchForm.content!='' && this.searchForm.content!=undefined){
        params['content'] = '%' + this.searchForm.content + '%'
      }
      this.$http({
        url: "discussjijieshipu/page",
        method: "get",
        params: params
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    }
  }
}
</script>
<style lang="scss" scoped>
.main-content {
  .slt {
    margin: 0 0 20px 0;
    display: flex;
    justify-content: flex-start;

    .el-form-item {
      margin-right: 15px;
      margin-bottom: 0;
    }
  }

  .ad {
    margin-bottom: 20px;
  }

  .el-button {
    margin-right: 8px;
  }

  .el-table {
    border-radius: 8px;
    overflow: hidden;

    .el-button {
      padding: 6px 10px;
      font-size: 12px;
      margin: 3px;
      min-width: 70px;
      border-radius: 3px;

      &.el-button--success {
        background-color: #4a90e2;
        border-color: #4a90e2;
      }

      &.el-button--primary {
        background-color: #4a90e2;
        border-color: #4a90e2;
      }

      &.el-button--danger {
        background-color: #ff3b30;
        border-color: #ff3b30;
      }

      i {
        margin-right: 3px;
      }
    }

    .el-table-column--operation {
      .cell {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
      }
    }
  }

  .pagination-content {
    display: flex;
    justify-content: center;
    margin-top: 20px;
  }
}

.table-content {
  margin-top: 20px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.tables {
  width: 100%;
}

.tables .el-table__header th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
  border-color: #ebeef5;
}

.tables .el-table__row:hover {
  background-color: #f5f7fa;
}

.tables .el-table__row td {
  padding: 10px 0;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}

.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.search-form .el-form-item {
  margin-bottom: 10px;
}

.search-form .el-input__inner {
  border-radius: 4px;
  border-color: #dcdfe6;
}

.search-form .el-input__inner:hover {
  border-color: #c0c4cc;
}

.search-form .el-input__inner:focus {
  border-color: #4a90e2;
}

.search-form .el-button {
  border-radius: 4px;
  padding: 10px 20px;
  transition: all 0.3s;
}

.search-form .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 操作按钮样式优化 */
.operation-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
  padding: 5px 0;
}

.operation-buttons .el-button {
  margin: 3px;
  flex-grow: 0;
  min-width: 74px;
  font-size: 12px;
  height: 32px;
  padding: 0 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.25s ease;
}

.operation-buttons .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.operation-buttons .el-button--success {
  background-color: #67c23a;
  border-color: #67c23a;
}

.operation-buttons .el-button--primary {
  background-color: #409eff;
  border-color: #409eff;
}

.operation-buttons .el-button--danger {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

/* 当表格列宽度不够时，确保操作按钮区域仍然美观 */
.operation-column {
  .cell {
    padding: 5px !important;
    overflow: visible !important;
  }
}
</style>
